<template>
  <basic-container>
    <avue-crud v-bind="bindVal"
               v-on="onEvent"
               v-model="form"
               ref="crud"
               @tree-load="treeLoad"
               :search.sync="searchFrom">

      <template slot="target" slot-scope="scope">
        <el-tag>{{scope.row.targetStr}}</el-tag>
      </template>

      <template slot="menuLeft" slot-scope="{}">
        <el-button type="danger" v-if="permission.delCodeShow===undefined?false:permission.delCodeShow" icon="el-icon-delete" size="small" @click.stop="deletes()">批量删除</el-button>
      </template>

      <template slot="menuBtn"
                slot-scope="scope">
        <el-dropdown-item divided
                          icon="el-icon-circle-plus-outline"
                          v-if="scope.row.isMenu === 1 && (permission.addCodeShow===undefined?false:permission.addCodeShow)"
                          @click.native="handleAdd(scope.row,scope.index)"
                          size="small">新增子项
        </el-dropdown-item>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>

export default window.$crudCommon({
  data () {
    return {
      searchFrom: {}
    }
  },
  created () {
  },
  methods: {
    treeLoad(tree, treeNode, resolve){
      this.api[this.option.list || 'list']({parentId_eq: tree.id}).then(res => {
        resolve(res.data.data);
      });
    },
    handleAdd (row) {
      this.$refs.crud.value.parentId = row.id;
      this.findObject(this.$refs.crud.option.column,"parentId").value = row.id;
      this.$refs.crud.rowAdd();
    },
    rowSave (row, done, loading) {
      this.api.add(row).then((data) => {
        this.getList()
        done();
      }).catch(() => {
        loading();
      })
    },
    rowUpdate (row, index, done, loading) {
      this.api.update(row.id, row, index).then((data) => {
        done(row)
      }).catch(() => {
        loading()
      })
    },
    rowDel (row, index, done, loading) {
      this.api.del(row.id, row).then((data) => {
        this.$message.success('删除成功')
        done(row);
      }).catch(() => {
        loading()
      })
    }
  }
}, {
  name: '/system/menuList',
  res: (data) => {
    return {
      data: data.data
    }
  },
  crudPage: false
})
</script>

<style lang="scss" scoped>

</style>
